From a8597ef08bcfa6963d600c3198df87abba4a7100 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 3 Apr 2018 00:11:38 +0300 Subject: [PATCH] Move filename logic to FileType --- .../cargo_rustc/context/compilation_files.rs | 42 ++++--------------- .../ops/cargo_rustc/context/target_info.rs | 28 ++++++++++--- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/context/compilation_files.rs b/src/cargo/ops/cargo_rustc/context/compilation_files.rs index 752b07cf6..81272fe8e 100644 --- a/src/cargo/ops/cargo_rustc/context/compilation_files.rs +++ b/src/cargo/ops/cargo_rustc/context/compilation_files.rs @@ -235,7 +235,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> { .map(|(ld, ls)| ld.join(format!("lib{}.rmeta", ls))); ret.push((filename, link_dst, FileFlavor::Linkable)); } else { - let mut add = |crate_type: &str, file_type: FileFlavor| -> CargoResult<()> { + let mut add = |crate_type: &str, flavor: FileFlavor| -> CargoResult<()> { let crate_type = if crate_type == "lib" { "rlib" } else { @@ -243,43 +243,19 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> { }; let file_types = info.file_types( crate_type, - file_type, + flavor, unit.target.kind(), cx.target_triple(), )?; match file_types { - Some(types) => { - for file_type in types { - // wasm bin target will generate two files in deps such as - // "web-stuff.js" and "web_stuff.wasm". Note the different usages of - // "-" and "_". should_replace_hyphens is a flag to indicate that - // we need to convert the stem "web-stuff" to "web_stuff", so we - // won't miss "web_stuff.wasm". - let conv = |s: String| { - if file_type.should_replace_hyphens { - s.replace("-", "_") - } else { - s - } - }; - let filename = out_dir.join(format!( - "{}{}{}", - file_type.prefix, - conv(file_stem.clone()), - file_type.suffix, - )); - let link_dst = link_stem.clone().map(|(ld, ls)| { - ld.join(format!( - "{}{}{}", - file_type.prefix, - conv(ls), - file_type.suffix - )) - }); - ret.push((filename, link_dst, file_type.flavor)); - } - } + Some(types) => for file_type in types { + let filename = out_dir.join(file_type.filename(&file_stem)); + let link_dst = link_stem + .as_ref() + .map(|&(ref ld, ref ls)| ld.join(file_type.filename(ls))); + ret.push((filename, link_dst, file_type.flavor)); + }, // not supported, don't worry about it None => { unsupported.push(crate_type.to_string()); diff --git a/src/cargo/ops/cargo_rustc/context/target_info.rs b/src/cargo/ops/cargo_rustc/context/target_info.rs index 6dbb3076b..309783953 100644 --- a/src/cargo/ops/cargo_rustc/context/target_info.rs +++ b/src/cargo/ops/cargo_rustc/context/target_info.rs @@ -28,10 +28,26 @@ pub enum FileFlavor { } pub struct FileType { - pub suffix: String, - pub prefix: String, pub flavor: FileFlavor, - pub should_replace_hyphens: bool, + suffix: String, + prefix: String, + // wasm bin target will generate two files in deps such as + // "web-stuff.js" and "web_stuff.wasm". Note the different usages of + // "-" and "_". should_replace_hyphens is a flag to indicate that + // we need to convert the stem "web-stuff" to "web_stuff", so we + // won't miss "web_stuff.wasm". + should_replace_hyphens: bool, +} + +impl FileType { + pub fn filename(&self, stem: &str) -> String { + let stem = if self.should_replace_hyphens { + stem.replace("-", "_") + } else { + stem.to_string() + }; + format!("{}{}{}", self.prefix, stem, self.suffix) + } } impl TargetInfo { @@ -126,7 +142,7 @@ impl TargetInfo { pub fn file_types( &self, crate_type: &str, - file_type: FileFlavor, + flavor: FileFlavor, kind: &TargetKind, target_triple: &str, ) -> CargoResult>> { @@ -145,9 +161,9 @@ impl TargetInfo { }; let mut ret = vec![ FileType { - suffix: suffix.to_string(), + suffix: suffix.clone(), prefix: prefix.clone(), - flavor: file_type, + flavor, should_replace_hyphens: false, }, ]; -- 2.30.2